﻿using MapleLeaf.Core.Extension;
using MapleLeaf.Model.CookBook.Menu.Entity;
using Mapster;

namespace MapleLeaf.Application.CookBook.Models {


    /// <summary>
    /// MenuVO，菜单视图对象
    /// </summary>
    public class MenuVo {
        /// <summary>
        /// 子菜单
        /// </summary>
        public List<MenuVo> Children { get; set; } = [];

        /// <summary>
        /// 组件路径
        /// </summary>
        public string? Component { get; set; }

        /// <summary>
        /// ICON
        /// </summary>
        public string? Icon { get; set; }

        /// <summary>
        /// 菜单ID 
        /// </summary>
        public string? Id { get; set; }

        /// <summary>
        /// 菜单名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 父菜单ID
        /// </summary>
        public string? ParentId { get; set; }

        /// <summary>
        /// 按钮权限标识
        /// </summary>
        public string? Perm { get; set; }

        /// <summary>
        /// 跳转路径
        /// </summary>
        public string? Redirect { get; set; }

        /// <summary>
        /// 路由名称
        /// </summary>
        public string? RouteName { get; set; }

        /// <summary>
        /// 路由路径
        /// </summary>
        public string? RoutePath { get; set; }

        /// <summary>
        /// 菜单排序(数字越小排名越靠前)
        /// </summary>
        public int? Sort { get; set; }

        /// <summary>
        /// 菜单类型
        /// </summary>
        public MenuTypeEnum? Type { get; set; }

        /// <summary>
        /// 菜单是否可见(1:显示;0:隐藏)
        /// </summary>
        public int? Visible { get; set; }


        /// <summary>
        /// 【菜单】是否开启页面缓存(1:是;0:否)
        /// </summary>
        public int? KeepAlive { get; set; }
    }
    /// <summary>
    /// 菜单扩展类
    /// </summary>
    public static class MenuVoExtension {
        /// <summary>
        /// 获取菜单树
        /// </summary>
        /// <param name="menus"></param>
        /// <returns></returns>
        public static List<MenuVo> GetTree(this List<MenuEntity> menus) {
            var list = menus.Where(m => m.ParentId == null || m.ParentId == 0).ToList();
            var voList = new List<MenuVo>();
            foreach (var item in list) {
                var menu = item.Adapt<MenuVo>();
                GetChildren(menu, menus);
                voList.Add(menu);
            }
            return voList;
        }
        /// <summary>
        /// 获取子菜单
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="menus"></param>
        private static void GetChildren(MenuVo parent, List<MenuEntity> menus) {
            var children = menus.Where(m => m.ParentId == parent.Id.ToLong()).ToList();
            foreach (var item in children) {
                var menu = item.Adapt<MenuVo>();
                GetChildren(menu, menus);
                parent.Children.Add(menu);
            }
        }

        /// <summary>
        /// 获取菜单选项树
        /// </summary>
        /// <param name="menus"></param>
        /// <returns></returns>
        public static List<MenuOptionVo> GetMenuOptionTree(this List<MenuEntity> menus) {
            var list = menus.Where(m => m.ParentId == null || m.ParentId == 0).ToList();
            var voList = new List<MenuOptionVo>();
            foreach (var item in list) {
                var menu = new MenuOptionVo() {
                    Label = item.Name,
                    Value = item.Id.ToString()
                };
                GetChildren(menu, menus);
                voList.Add(menu);
            }
            return voList;
        }
        /// <summary>
        /// 获取子菜单
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="menus"></param>
        private static void GetChildren(MenuOptionVo parent, List<MenuEntity> menus) {
            var children = menus.Where(m => m.ParentId == parent.Value.ToLong()).ToList();
            foreach (var item in children) {
                var menu = new MenuOptionVo() { 
                    Label = item.Name,
                    Value = item.Id.ToString()
                };
                GetChildren(menu, menus);
                parent.Children.Add(menu);
            }
        }
    }


    /// <summary>
    /// 菜单选项
    /// </summary>
    public class MenuOptionVo() {
        /// <summary>
        /// 选项
        /// </summary>
        public string Label { get; set; }
        /// <summary>
        /// 选项值
        /// </summary>
        public string Value { get; set; }
        /// <summary>
        /// 子选项
        /// </summary>
        public List<MenuOptionVo> Children { get; set; } = [];

    }







    /// <summary>
    /// 菜单类型（1-菜单 2-目录 3-外链 4-按钮）
    /// </summary>
    public enum MenuTypeEnum {
        /// <summary>
        /// 
        /// </summary>
        Null = 0,
        /// <summary>
        /// 菜单
        /// </summary>
        Menu = 1,
        /// <summary>
        /// 目录
        /// </summary>
        Catalog = 2,
        /// <summary>
        /// 外链
        /// </summary>
        Extlink = 3,
        /// <summary>
        /// 按钮
        /// </summary>
        Button = 4
    };


}
